package cn.lliiooll.launcher.update;

import cn.lliiooll.launcher.log.LogOut;

import javax.swing.*;
import java.io.*;
import java.net.Socket;
import java.net.UnknownHostException;

public class Update{
	
	public static int port = 10943;
	public static String link = "lliiooll.tpddns.cn";
	private static String nv;
	private static String version;
	
	public static String getUpdate() {
		
		try {
			Socket c = new Socket("lliiooll.tpddns.cn",port);
			//OutputStream ots = c.getOutputStream();
			//DataOutputStream out = new DataOutputStream(ots);
			//out.writeUTF("1");
			InputStream ifs = c.getInputStream();
			DataInputStream in = new DataInputStream(ifs);
			OutputStream outs = c.getOutputStream();
			DataOutputStream out = new DataOutputStream(outs);
			out.writeInt(0);
			String ver = new String(in.readUTF());
			File nversionfilepath = new File("tmp");
			File nversionfile = new File(nversionfilepath + "\\newversion.txt");
			if(!(nversionfile.exists())) {
				nversionfilepath.mkdir();
				nversionfile.createNewFile();
			FileOutputStream nversion = new FileOutputStream(nversionfile);
				
				try {
					nversion.write(ver.getBytes());
					nversion.close();
					c.close();
				} catch (Exception e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}else {
			FileOutputStream nversion = new FileOutputStream(nversionfile);
				
				try {
					nversion.write(ver.getBytes());
					nversion.close();
					c.close();
				} catch (Exception e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
				}
			}
				try {
					FileInputStream nversion1;
					byte[] b = new byte[5];
					int i = 0;
					int index = 0;
					
					if(!(nversionfile.exists())) {
						getUpdate();
						return nv;
					}else {
						nversion1 = new FileInputStream(nversionfile);
						try {
							while((i = nversion1.read()) != -1) {
								b[index]=(byte) i;
								index++;
							}
							nversion1.close();
							String v1 = new String(b);
							nv = v1.replaceAll(" ", ""); 
							nversionfile.delete();
							} catch (Exception e) {
							// TODO 自动生成的 catch 块
							e.printStackTrace();
						}
						version = Version.getVersion();
						version = version.replaceAll(" ", ""); 
						if(version.equals(nv)) {
							LogOut.info("版本是" + nv);
							LogOut.info("版本是" + version);
							return nv;
						}else if(version.equals("null")){
							LogOut.info("版本是" + nv);
							LogOut.info("版本是" + version);
							return "null";
						}else {
							LogOut.info("版本是" + nv);
							LogOut.info("版本是" + version);
							return "update";
						}
					}


				} catch (FileNotFoundException e) {
					// TODO 自动生成的 catch 块
					e.printStackTrace();
					return "null";
				}
		} catch (UnknownHostException e) {
			LogOut.error("未知的主机.");
			JOptionPane.showMessageDialog(null,"无法连接服务器", "错误",JOptionPane.ERROR_MESSAGE); 
			e.printStackTrace();
			return "null";
		} catch (IOException e) {
			JOptionPane.showMessageDialog(null,"无法连接服务器", "错误",JOptionPane.ERROR_MESSAGE); 
			e.printStackTrace();
			return "null";
		}
	} 
	
	public static void update() {
		//if(getUpdate().equals("noupdate")) {
			//return "noupdate";
		//}else {
			//return null;
		//}
		LogOut.info("开始更新..");
		Download.download("download", "update.zip");
		
		
	}
}
